package com.lottery.service.pc28.statisticsService.impl;

import com.lottery.dao.pc28.lobbyDao.LotteryDao;
import com.lottery.dao.pc28.statisticsDao.PlaywayMoneyDao;
import com.lottery.dto.ResultDto;
import com.lottery.entity.PlaywayMoney;
import com.lottery.service.pc28.statisticsService.PlaywayMoneyService;
import com.lottery.service.pc28.wealthService.impl.uitil.DateUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.*;

@Service
public class PlaywayMoneyServiceImpl implements PlaywayMoneyService {

	@Resource
	private PlaywayMoneyDao playwayMoneyDao;

	@Resource
	private LotteryDao lotteryDao;

	/**
	 * 插入数据
	 * 
	 * @param expect
	 *            期号
	 * @return
	 */

	public ResultDto<PlaywayMoney> insetPlaywayMoney(String expect) {
		ResultDto<PlaywayMoney> resultDto = new ResultDto<PlaywayMoney>();
		PlaywayMoney playwayMoney = null;
		List<PlaywayMoney> list = playwayMoneyDao.getPlaywayMoneyLimit(expect);
		if (list != null && list.size() > 0) {
			for (PlaywayMoney playwayMoneyLimit : list) {
				playwayMoney = new PlaywayMoney();
				// 投注总金额
				double bettingmoney = playwayMoneyLimit.getBettingmoney();
				// 中奖总金额
				double winTotalmoney = playwayMoneyLimit.getWinTotalmoney();
				// 收益
				double income = 0;
				PlaywayMoney pm = new PlaywayMoney();
				pm.setExpect(expect);
				pm.setPlaywayinfoId(playwayMoneyLimit.getPlaywayinfoId());
				double bettingmoney2 = playwayMoneyDao.getBettingmoney(pm);
				double winningamount2 = playwayMoneyDao.getWinningamount(pm);
				income = bettingmoney2 - winningamount2;
				DecimalFormat df = new DecimalFormat("#0.##");
				df.setRoundingMode(RoundingMode.FLOOR);
				income = Double.valueOf(df.format(income));
				// 玩法详情id
				int playwayinfoId = playwayMoneyLimit.getPlaywayinfoId();
				playwayMoney.setExpect(expect);
				playwayMoney.setBettingmoney(bettingmoney);
				playwayMoney.setWinTotalmoney(winTotalmoney);
				playwayMoney.setIncome(income);
				playwayMoney.setPlaywayinfoId(playwayinfoId);
				playwayMoney.setPlaywayDate(new Date());
				try {
					playwayMoneyDao.insertPlaywayMoney(playwayMoney);
					resultDto.setMessage("200");
					resultDto.setSuccess(true);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					resultDto.setMessage("400" + e.toString());
					resultDto.setSuccess(false);
					return resultDto;
				}
			}
		}

		return resultDto;
	}

	/**
	 * 规则大类统计接口
	 * 
	 * @return
	 */

	public ResultDto<List<Map<String, String>>> getPlaywayMoney() {
		ResultDto<List<Map<String, String>>> resultDto = new ResultDto<List<Map<String, String>>>();
		// 结果list
		List<Map<String, String>> list = new ArrayList<Map<String, String>>();
		// 期号降序排列最新5期的记录
		List<PlaywayMoney> expectlist = playwayMoneyDao.selectPlaywayMoneyExpect();
		Map<String, String> map = new HashMap<String, String>();
		if (expectlist == null || expectlist.size() == 0) {
			map.put("date", "0");// 保存时间
			map.put("sizeDanShuang", "0.0");// 保存
			map.put("number", "0.0");
			map.put("digitalCombination", "0.0");// 保存
			map.put("digitalPositioning", "0.0");// 保存
			map.put("guessGe", "0.0");// 保存
			list.add(map);
			resultDto.setList(list);
			resultDto.setSuccess(true);
		} else {
			// 反转
			Collections.reverse(expectlist);
			try {
				for (int i = 0; i < expectlist.size(); i++) {

					String expect = expectlist.get(i).getExpect();
					PlaywayMoney playwayMoney = new PlaywayMoney();
					playwayMoney.setExpect(expect);
					// 获取当前日期的开奖时间
					Date opentime = lotteryDao.selectLotteryByExpect(expect).getOpentime();
					String date = DateUtil.formatDate(opentime, DateUtil.YMDHM);
					map.put("date", date);// 保存时间

					playwayMoney.setPlaywayinfoId(1);// 大小单双玩法
					double sizeSingleMoney = 0.0;// 大小单双玩法投注总金额
					PlaywayMoney sizeSingle = playwayMoneyDao.getPlaywayMoney(playwayMoney);
					if (sizeSingle != null) {
						sizeSingleMoney = sizeSingle.getBettingmoney();// 设置投注总金额
					}
					map.put("sizeDanShuang", Double.toString(sizeSingleMoney));// 保存

					playwayMoney.setPlaywayinfoId(2);// 数字玩法
					double numberMoney = 0.0;// 数字玩法投注总金额
					PlaywayMoney number = playwayMoneyDao.getPlaywayMoney(playwayMoney);
					if (number != null) {
						numberMoney = number.getBettingmoney();// 设置投注总金额
					}
					map.put("number", Double.toString(numberMoney));

					playwayMoney.setPlaywayinfoId(3);// 数字组合玩法
					double splitBetMoney = 0.0;// 数字组合玩法投注总金额
					PlaywayMoney splitBet = playwayMoneyDao.getPlaywayMoney(playwayMoney);
					if (splitBet != null) {
						splitBetMoney = splitBet.getBettingmoney();// 设置投注总金额
					}
					map.put("digitalCombination", Double.toString(splitBetMoney));// 保存

					playwayMoney.setPlaywayinfoId(4);// 数字定位玩法
					double figureOrientedMoney = 0.0;// 数字定位玩法投注总金额
					PlaywayMoney figureOriented = playwayMoneyDao.getPlaywayMoney(playwayMoney);
					if (figureOriented != null) {
						figureOrientedMoney = figureOriented.getBettingmoney();// 设置投注总金额
					}
					map.put("digitalPositioning", Double.toString(figureOrientedMoney));// 保存

					playwayMoney.setPlaywayinfoId(5);// 猜合玩法
					double guessCloseMoney = 0.0;// 猜合玩法投注总金额
					PlaywayMoney guessClose = playwayMoneyDao.getPlaywayMoney(playwayMoney);
					if (guessClose != null) {
						guessCloseMoney = guessClose.getBettingmoney();// 设置投注总金额
					}
					map.put("guessGe", Double.toString(guessCloseMoney));// 保存
					list.add(map);
				}
				resultDto.setList(list);
				resultDto.setSuccess(true);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				resultDto.setSuccess(false);
			}
		}
		return resultDto;
	}

}
